package com.example.gkcircle.fragment.lost.search;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseFragment;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.LostAdapter;
import com.example.gkcircle.bean.Lost;
import com.example.gkcircle.utils.DateUtil;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.ToastFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 失物Fragment
 * @author ZhouHua
 * created at 15/12/21 上午12:57
 */
public class SearchLostFragment extends BaseFragment  implements AbsListView.OnScrollListener{

    private ListView mListView;
    private SwipeRefreshLayout mSwipe;
    private LostAdapter mAdapter;
    private static SearchLostFragment instance;

    public static int currentPage=0;
    public static final int maxDataCount=10;

    private SearchLostFragment() {
    }

    public static SearchLostFragment getInstance() {
        if (instance == null) instance = new SearchLostFragment();
        return instance;
    }

    @Override
    protected int setLayout() {
        return R.layout.home_lost_fragment;
    }

    @Override
    protected void initView(View view) {
        mListView=(ListView)view.findViewById(R.id.lost_listview);
        mSwipe=(SwipeRefreshLayout)view.findViewById(R.id.lost_swipe);
        mSwipe.setColorSchemeColors(getResources().getColor(R.color.main), getResources().getColor(R.color.love), getResources().getColor(R.color.wish));

    }


    @Override
    protected void initListener() {
        mListView.setOnScrollListener(this);
        //下拉刷新
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipe.setRefreshing(false);
            }
        });

        //拨打电话回调显示AlertView
        SearchLostFragment.onShowAlertViewListener=new OnShowAlertViewListener() {
            @Override
            public void show(final String telNumber) {
                MyAlertView.showAlertViewWithOKAndCancel(getActivity(), "是否拨打  " + telNumber, new MyAlertView.IShowAlertViewWithOkAndCancelListener() {
                    @Override
                    public void onclickOk() {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNumber));
                        getActivity().startActivity(intent);
                    }
                });
                MyAlertView.alertView=null;
            }
        };
    }

    //上拉加载
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState){
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                if(mListView.getLastVisiblePosition()==(mListView.getCount()-1)){
                    SearchLostFragment.currentPage++;
                    setData();
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    @Override
    protected void setData() {
        getData(new IGetDataListener() {
            @Override
            public void complete(List<AVObject> list,String keyword) {
                if(list==null){
                    currentPage--;
                    return;
                }
                if (App.mSearchLostList == null) {
                    App.mSearchLostList = new ArrayList<Lost>();
                }
                for (int i = 0; i < list.size(); i++) {
                    Lost lost = new Lost();
                    lost.setUser(list.get(i).getAVUser("user"));
                    lost.setContacts(list.get(i).get("contacts").toString());
                    lost.setContent(list.get(i).get("content").toString());
                    lost.setCreatedAt(DateUtil.getLastMinTime(list.get(i).getDate("createdAt").getTime()));
                    lost.setPhotoUrl(list.get(i).getAVFile("photo") == null ? "" : list.get(i).getAVFile("photo").getUrl());
                    App.mSearchLostList.add(lost);
                }
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                    mSwipe.setRefreshing(false);
                    return;
                }
                mSwipe.setRefreshing(false);
                mAdapter = new LostAdapter(getActivity(), App.mSearchLostList,LostAdapter.SEARCH_LOST_FLAG);
                mListView.setAdapter(mAdapter);
            }
        });
    }

    //拨打电话回调
    public static OnShowAlertViewListener onShowAlertViewListener;

    public interface OnShowAlertViewListener{
        void show(String telNumber);
    }

    //失物搜索回调(SearchLostActivity)
    public static ISetSearchLostListener iSetSearchLostListener;
    public interface ISetSearchLostListener{
        void complete(List<AVObject> list,AVException e,String keyword);
    }
    public void setSearchLostListener(ISetSearchLostListener iSetSearchLostListener){
        SearchLostFragment.iSetSearchLostListener=iSetSearchLostListener;
    }

    //获取数据回调
    public IGetDataListener getDataListener;

    private interface IGetDataListener{
        void complete(List<AVObject> list,String keyword);
    }

    private void getData(final IGetDataListener getDataListener){
        this.getDataListener=getDataListener;
        setSearchLostListener(new ISetSearchLostListener() {
            @Override
            public void complete(List<AVObject> list, AVException e,String keyword) {

                if (e == null) {
                    if (list.size() > 0) {
                        if(dataFlag)getDataListener.complete(list,keyword);else return;
                    } else {
                        if(dataFlag)getDataListener.complete(null, null);else return;
                        mSwipe.setRefreshing(false);
                        ToastFactory.getToast(getActivity(), getResources().getString(R.string.no_more_data)).show();
                    }
                } else {
                    mSwipe.setRefreshing(false);
                    if(null!=getDataListener)getDataListener.complete(null,null);
                    ToastFactory.getToast(getActivity(), getResources().getString(R.string.connection_error)).show();
                }

            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        if(App.mSearchLostList!=null){
            App.mSearchLostList.clear();
        }
    }
}
